
package it.csttech.date;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * OVERVIEW This class provides a json time serializer only when exception
 * occurs
 * 
 * @author marcello.felappi
 * @author marco.dalena
 */
public class DateTimeSerializer extends JsonSerializer<Date> {

	/**
	 * Method that performs serialization; accepts a date, a json generator and
	 * a serialize provider. The method creates a DateFormat for time and
	 * seriliaze it.
	 * 
	 * @param Date
	 *            - input date to be serialized, it will serialize a time
	 * @param JsonGenerator
	 *            - generator used for serializing a json file
	 * @param SerializerProvider
	 *            - provider in which the date serializer will be registered
	 */
	@Override
	public void serialize(
		Date date, JsonGenerator jsonGenerator, SerializerProvider provider)
		throws IOException, JsonProcessingException {

		DateFormat dFormat = null;

		dFormat = new SimpleDateFormat(" HH:mm:ss z");

		jsonGenerator.writeString(dFormat.format(date));

	}

}
